package com.xx.web.controller.file;

import com.xx.common.annotation.Log;
import com.xx.common.core.controller.BaseController;
import com.xx.common.core.domain.AjaxResult;
import com.xx.common.core.page.TableDataInfo;
import com.xx.common.enums.BusinessType;
import com.xx.common.utils.poi.ExcelUtil;
import com.xx.file.domain.AppFile;
import com.xx.file.service.IAppFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 附件Controller
 *
 * @author anqi
 * @date 2024-06-29
 */
@Api(tags = "附件")
@RestController
@RequestMapping("/file/appfile")
public class AppFileController extends BaseController {

    @Autowired
    private IAppFileService appFileService;

    /**
     * 查询附件列表
     */
    @PreAuthorize("@ss.hasPermi('file:appfile:list')")
    @GetMapping("/list")
    @ApiOperation("查询附件列表")
    public TableDataInfo list(AppFile appFile) {
        startPage();
        List<AppFile> list = appFileService.selectAppFileList(appFile);
        return getDataTable(list);
    }

    /**
     * 导出附件列表
     */
    @PreAuthorize("@ss.hasPermi('file:appfile:export')")
    @Log(title = "附件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AppFile appFile) {
        List<AppFile> list = appFileService.selectAppFileList(appFile);
        ExcelUtil<AppFile> util = new ExcelUtil<AppFile>(AppFile.class);
        util.exportExcel(response, list, "附件数据");
    }

    /**
     * 获取附件详细信息
     */
    @PreAuthorize("@ss.hasPermi('file:appfile:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(appFileService.selectAppFileById(id));
    }

    /**
     * 新增附件
     */
    @PreAuthorize("@ss.hasPermi('file:appfile:add')")
    @Log(title = "附件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AppFile appFile) {
        return toAjax(appFileService.insertAppFile(appFile));
    }

    /**
     * 修改附件
     */
    @PreAuthorize("@ss.hasPermi('file:appfile:edit')")
    @Log(title = "附件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppFile appFile) {
        return toAjax(appFileService.updateAppFile(appFile));
    }

    /**
     * 删除附件
     */
    @PreAuthorize("@ss.hasPermi('file:appfile:remove')")
    @Log(title = "附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(appFileService.deleteAppFileByIds(ids));
    }

}
